package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import dao.UserDao;
import util.DBUtils;
import vo.User;

public class UserDaoImpl implements UserDao {

	@Override
	public void save(User user) throws SQLException {
		String sql = "insert into staff(name,password)values(?,?)";
		QueryRunner qr = new QueryRunner();
		qr.update(DBUtils.getConnection(),sql,user.getName(),user.getPassword());
	}

	@Override
	public int delete(int id) throws SQLException {
		return 0;
	}

	@Override
	public int update(User user) throws SQLException {
		return 0;
	}

	@Override
	public User findById(int id) throws SQLException {
		String sql = "select id,name,age from person where id =?";
		QueryRunner qr = new QueryRunner();
		User user = qr.query(DBUtils.getConnection(), sql,  new BeanHandler<User>(User.class),id);
		return user;
	}

	@Override
	public List<User> find() throws SQLException {
		return null;
	}

	@Override
	public boolean find(String username, String password) throws SQLException {
		boolean result = false;
		User user = null;
		QueryRunner qr = new QueryRunner();

		String sql = "select name,password from staff where name=? and password=?";
		String params[] = new String[2];
		params[0] = username;
		params[1] = password;

		try {
			user = qr.query(DBUtils.getConnection(), sql, new BeanHandler<User>(User.class), params);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if (user != null) {
			result = true;
			System.out.println(user.getName() + "  " + user.getPassword());
		}

		return result;
	}


}
